/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel;

import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.balisunrise.swing.components.panel.layouts.EntityActionLayoutBuilder;

/**
 *
 * @author Glauco
 */
public class ActionPanel extends JPanel{

    private JButton cancelButton;
    private JButton confirmButton;
    private List<JButton> actions;
    
    private EntityActionLayoutBuilder layoutBuilder;

    public ActionPanel(EntityActionLayoutBuilder layoutBuilder) {
        super();
        this.layoutBuilder = layoutBuilder;
        actions = layoutBuilder.getActions();
        cancelButton = layoutBuilder.getCancelButton();
        confirmButton = layoutBuilder.getConfirmButton();
        createLayout();
    }

    public void setConfirmationButton(JButton button) {
        confirmButton = button;
        createLayout();
    }

    public void setCancellationButton(JButton button) {
        cancelButton = button;
        createLayout();
    }

    public void addAction(JButton button, int index) {

        if (actions.size() < index) {
            actions.add(button);
        }else if (index < 0) {
            actions.add(0, button);
        }else
            actions.add(index, button);

        createLayout();
    }

    public void setActions(List<JButton> actions) {
        this.actions = actions;
        createLayout();
    }

    private void createLayout() {

        layoutBuilder.setConfirmButton(confirmButton);
        layoutBuilder.setCancelButton(cancelButton);
        layoutBuilder.setActions(actions);

        layoutBuilder.buildLayout(this);
    }
}
